package org.jash.mvp2206.news;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.jash.mvp2206.R;
import org.jash.mvp2206.entity.CommentEntity;
import org.jash.mvp2206.net.ApiServer;
import org.jash.mvp2206.utils.RetrofitUtils;

import java.util.HashMap;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.Disposable;


public class CommentFragment extends BottomSheetDialogFragment {
    private RecyclerView recycler;
    private CommentAdapter adapter;
    private ApiServer server;
    private int nid;
    private Disposable disposable;
    private EditText content;

    public CommentFragment() {
        super(R.layout.fragment_comment);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = view.findViewById(R.id.recycler);
        content = view.findViewById(R.id.content);
        adapter = new CommentAdapter();
        recycler.setAdapter(adapter);
        server = RetrofitUtils.getRetrofit().create(ApiServer.class);
        MaterialToolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(v -> dismiss());
        disposable = server.getCommentByNewsId(nid).observeOn(AndroidSchedulers.mainThread())
                .subscribe(r -> {
                    if (r.getCode() == 0) {
                        adapter.addAll(r.getData());
                    }
                }, t -> {
                    Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                });
        view.findViewById(R.id.send).setOnClickListener(v -> {
            disposable = server.sendComment(new HashMap<String, Object>(){{
                put("content", content.getText().toString());
                put("nid", nid);
                put("parentid", 0);
            }}).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(r -> {
                        if (r.getCode() == 0) {
                            Toast.makeText(requireContext(), "发布成功", Toast.LENGTH_SHORT).show();
                            CommentEntity.DataBean bean = new CommentEntity.DataBean();
                            bean.setContent(content.getText().toString());
                            adapter.addAll(List.of(bean));
                            content.setText("");
                        }
                    }, t -> {
                        Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                    });
        });

    }

    public void setNid(int nid) {
        this.nid = nid;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}